<?php
namespace Admin\Controller;
use Think\Controller;
class CjController extends CommonController 
{
  //抽奖用户
    public function cj_user()
    {
        //分页显示
        $user_cj_user = M('user_cj_user'); // 实例化User对象
        $count      = $user_cj_user->count();// 查询满足要求的总记录数
        $this->assign('num',$count);
        $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $user_cj_user->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);
        $this->assign('page',$show);// 赋值分页输出

        $this->display();
    }
    //批量删除
    public function cj_user_dels()
    {
      //Array ( [arr] => Array ( [0] => 1 [1] => 2 ) )
      if (IS_POST) {
        $arr = I('post.arr');
        $ids = implode(",", $arr);

        //删除
        $res = M('user_cj_user')->delete($ids);
          if ($res) {
              $arr = array(
                  'error' =>1,
                  'info'=>"删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>0,
                  'info'=>"删除失败"
                  );
              echo json_encode($arr);
          }
        }
    }   
    //彻底删除
    public function cj_user_del()
    {
      if (IS_POST) {
          $id = I('post.id');

          $sql = 'delete from user_cj_user where id='.$id;
          $is_del = M()->execute($sql);
          if ($is_del) {
              $arr = array(
                  'error' =>1,
                  'info'=>"彻底删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>0,
                  'info'=>"彻底删除失败"
                  );
              echo json_encode($arr);
          }
        }
    }


	  //抽奖列表
    public function index()
    {
      if (IS_POST) {
        $name = I('post.name');

        $count = M('user_cj_info')->field('user_cj_info.id,user_cj_info.phone_id,user_cj_info.num,user_cj_info.cj_time')->join("user_cj_user t2 on user_cj_info.phone_id = t2.id")->where(array('t2.phone'=>$name))->count();
        $this->assign('num',$count);

        $list = M('user_cj_info')->field('user_cj_info.id,user_cj_info.phone_id,user_cj_info.num,user_cj_info.cj_time')->join("user_cj_user t2 on user_cj_info.phone_id = t2.id")->where(array('t2.phone'=>$name))->select();
        $this->assign('list',$list);
      }else {
        //分页显示
        $user_cj_info = M('user_cj_info'); // 实例化User对象
        $count      = $user_cj_info->count();// 查询满足要求的总记录数
        $this->assign('num',$count);
        $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $user_cj_info->limit($Page->firstRow.','.$Page->listRows)->order('id asc')->select();
        $this->assign('list',$list);
        $this->assign('page',$show);// 赋值分页输出
      }
        $this->display();
    }
    //奖品发放状态
    public function is_fafang_ok()
    {
      if (IS_POST) {
        $id = I('post.id');
        $res = M('user_cj_info')->where(array('id'=>$id))->setField('is_fafang',1);
        if ($res) {
          $arr = array(
                'error' => 1,
                'info' => '发放成功'
            );
          echo json_encode($arr);

        }else {
          $arr = array(
                'error' => 0,
                'info' => '发放失败'
            );
          echo json_encode($arr);
        }
      }
    }
    //奖品取消状态
    public function is_fafang_no()
    {
      if (IS_POST) {
        $id = I('post.id');
        $res = M('user_cj_info')->where(array('id'=>$id))->setField('is_fafang',0);
        if ($res) {
          $arr = array(
                'error' => 1,
                'info' => '取消成功'
            );
          echo json_encode($arr);

        }else {
          $arr = array(
                'error' => 0,
                'info' => '取消失败'
            );
          echo json_encode($arr);
        }
      }
    }
    //批量删除
    public function index_dels()
    {
      //Array ( [arr] => Array ( [0] => 1 [1] => 2 ) )
      if (IS_POST) {
        $arr = I('post.arr');
        $ids = implode(",", $arr);

        //删除
        $res = M('user_cj_info')->delete($ids);
          if ($res) {
              $arr = array(
                  'error' =>1,
                  'info'=>"删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>0,
                  'info'=>"删除失败"
                  );
              echo json_encode($arr);
          }
        }
    }   
    //彻底删除
    public function index_del()
    {
      if (IS_POST) {
          $id = I('post.id');

          $sql = 'delete from user_cj_info where id='.$id;
          $is_del = M()->execute($sql);
          if ($is_del) {
              $arr = array(
                  'error' =>1,
                  'info'=>"彻底删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>0,
                  'info'=>"彻底删除失败"
                  );
              echo json_encode($arr);
          }
        }
    }


    //抽奖设置
    public function set()
    {
      $set = M('user_cj_lw')->order('id asc')->select();
      $this->assign('set',$set);

      $this->display();
    }
    //编辑显示
    public function set_edit()
    {
      $id = I('get.id');
      $set_info = M('user_cj_lw')->where(array('id'=>$id))->find();
      $this->assign('set_info',$set_info);

      //获取当前奖品 中奖信息
      $cj_info = M('user_cj_info')->where(array('num'=>$id))->select();
      $this->assign('cj_info',$cj_info);

      $this->display();
    }
    //编辑
    public function set_edit_add()
    {
      if (IS_POST) {
         $id = I('post.id');

         $data['lw_name'] = I('post.lw_name');
         $data['v'] = I('post.v');
         $data['time'] = time();

         //入库
         $res = M('user_cj_lw')->where(array('id'=>$id))->save($data);
          if ($res) {
              $arr = array(
                  'error' =>1,
                  'info'=>"修改成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>0,
                  'info'=>"修改失败"
                  );
              echo json_encode($arr);
          }
      }
    }


    
}